package com.example.nfc;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.nfc.activity.MainActivity;
import com.example.nfc.base.BaseNfcActivity;
import com.example.nfc.server.Const;
import com.example.nfc.server.network.http.HttpException;
import com.example.nfc.server.network.http.SyncHttpClient;
import com.example.nfc.server.request.LoLoginRequest;
import com.example.nfc.server.response.LoLoginRespone;
import com.example.nfc.tools.Base64Util;


import org.apache.http.impl.cookie.BasicClientCookie;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseNfcActivity {
    private TextView ifo_NFC;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ifo_NFC = findViewById(R.id.ifo_NFC);
        // NFC适配器，所有的关于NFC的操作从该适配器进行
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (!ifNFCUse()) {
        }
        String uname = getData(Const.USERNAME,"");
        if(!TextUtils.isEmpty(uname)){
            username.setText(uname);
        }
        String pwd = getData(Const.PASSWORD,"");
        if(!TextUtils.isEmpty(pwd)){
            password.setText(pwd);
        }
        loginRequest = new LoLoginRequest();
       // boolean isAdmin = getData(Const.IS_ADMIN,true);
//        if(isAdmin){
//            user.setType("password");
//            CommonUtils.setRadioButtonDrawable(this,rg,rb_1,0);
//        }else{
//            user.setType("seller");
//            CommonUtils.setRadioButtonDrawable(this,rg,rb_2,1);
//        }

      //  request(GET_TOKEN);
//        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                RadioButton radioButton = group.findViewById(checkedId);
//                int position = 0 ;
//                if(checkedId==R.id.rb_1){
//                    position = 0 ;
//                    user.setType("password");
//                    putData(Const.IS_ADMIN,true);
//                }else{
//                    position = 1 ;
//                    user.setType("seller");
//                    putData(Const.IS_ADMIN,false);
//                }
//                CommonUtils.setRadioButtonDrawable(LoginActivity.this,rg,radioButton,position);
//            }
//        });
    }
    @Bind(R.id.rg)
    RadioGroup rg;
    @Bind(R.id.rb_1)
    RadioButton rb_1;
    @Bind(R.id.rb_2)
    RadioButton rb_2;
    @Bind(R.id.username)
    EditText username;
    @Bind(R.id.password)
    EditText password;
    @OnClick(R.id.login_btn)
    public void login_btnClick(){
        if(TextUtils.isEmpty(username.getText().toString())){
            shortToast("请输入账号!");
            return;
        }
        if(TextUtils.isEmpty(password.getText().toString())){
            shortToast("请输入密码!");
            return;
        }
        loginRequest.setU(Base64Util.getInstance().urlEncode2(username.getText().toString()));
        loginRequest.setP(Base64Util.getInstance().urlEncode2(password.getText().toString()));
        loginRequest.setIsNew("1");
        loginRequest.setCode("");
        request(LOGIN_REQUEST);
        ProgressDialog.show(this,"正在登录中..");
    }

    private static final int LOGIN_REQUEST = 1001;
    private static final int GET_TOKEN = 1002;
    private LoLoginRequest loginRequest;
    @Override
    public Object doInBackground(int requestCode, String parameter) throws HttpException {
        switch (requestCode){
            case GET_TOKEN:
                return action.getTokenRequest();
            case LOGIN_REQUEST:
                return action.loginRequest(loginRequest);
        }
        return super.doInBackground(requestCode, parameter);
    }

    @Override
    public void onSuccess(int requestCode, Object result) {
        super.onSuccess(requestCode, result);
        switch (requestCode){

            case LOGIN_REQUEST:
                LoLoginRespone respone = (LoLoginRespone) result;
                ProgressDialog.disMiss();
                if("OK".equals(respone.getCode())){
                    putData(Const.USERNAME,username.getText().toString());
                    putData(Const.PASSWORD,password.getText().toString());

                    BasicClientCookie newCookie = new BasicClientCookie("phone", username.getText().toString());
                    SyncHttpClient.cookieStore.addCookie(newCookie);
                    BasicClientCookie newCookie1 = new BasicClientCookie("password", Base64Util.encode(Base64Util.encode(password.getText().toString())));
                    SyncHttpClient.cookieStore.addCookie(newCookie1);

                    BasicClientCookie newCookie2 = new BasicClientCookie("je-local-lang","zh_CN");
                    SyncHttpClient.cookieStore.addCookie(newCookie2);

                    BasicClientCookie newCookie3 = new BasicClientCookie("authorization",respone.getObj());
                    SyncHttpClient.cookieStore.addCookie(newCookie3);
//                    if(user.getType().equals("password")) {
//                        startActivityAndFinish(AdminActivity.class);
//                    }else {
//
//                    }
                    startActivityAndFinish(MainActivity.class);
                }else{
                    shortToast(respone.getMessage());
                }
                break;
        }
    }

    @Override
    public void onFailure(int requestCode, int state, Object result) {
        super.onFailure(requestCode, state, result);
        switch (requestCode){
            case LOGIN_REQUEST:
                ProgressDialog.disMiss();
                break;
        }
    }

    /**
     * 检测工作,判断设备的NFC支持情况
     *
     * @return
     */
    protected Boolean ifNFCUse() {
        if (mNfcAdapter == null) {
            ifo_NFC.setText("温馨提示：当前设备不支持NFC！");
            return false;
        }
        if (mNfcAdapter != null && !mNfcAdapter.isEnabled()) {
            ifo_NFC.setText("温馨提示：请在系统设置中先启用NFC功能！");
            return false;
        }
        return true;
    }
}

